home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / graphic / dbwrendr.zip / SOURCE / FIL.C < prev    next >
Text File  |  1989-10-31  |  19KB  |  549 lines

  1. /************************************************************************
  2.  *                                                                      *
  3.  *                  Copyright (c) 1987, David B. Wecker                 *
  4.  *                          All Rights Reserved                         *
  5.  *                                                                      *
  6.  * This file is part of DBW_Render                                      *
  7.  *                                                                      *
  8.  * DBW_Render is distributed in the hope that it will be useful, but    *
  9.  * WITHOUT ANY WARRANTY. No author or distributor accepts               *
  10.  * responsibility to anyone for the consequences of using it or for     *
  11.  * whether it serves any particular purpose or works at all, unless     *
  12.  * he says so in writing. Refer to the DBW_Render General Public        *
  13.  * License for full details.                                            *
  14.  *                                                                      *
  15.  * Everyone is granted permission to copy, modify and redistribute      *
  16.  * DBW_Render, but only under the conditions described in the           *
  17.  * DBW_Render General Public License. A copy of this license is         *
  18.  * supposed to have been given to you along with DBW_Render so you      *
  19.  * can know your rights and responsibilities. It should be in a file    *
  20.  * named COPYING. Among other things, the copyright notice and this     *
  21.  * notice must be preserved on all copies.                              *
  22.  ************************************************************************
  23.  *                                                                      *
  24.  * Authors:                                                             *
  25.  *      DBW - David B. Wecker                                           *
  26.  *      jhl - John H. Lowery                                            *
  27.  *                                                                      *
  28.  * Versions:                                                            *
  29.  *      V1.0  870125 DBW - First released version                       *
  30.  *      V1.01 880916 jhl - IBM PC (VGA/MCGA) conversion                 *
  31.  *            890121 jhl - MAXROW & MAXCOL become variables, add        *
  32.  *                         'D' command for Display max <hor> <vert>     *
  33.  *      V1.02 891031 jhl - add MAXCOL and MAXROW as first two integers  *
  34.  *                         in output file, so multiple formats can be   *
  35.  *                         supported.                                   * 
  36.  *                                                                      *
  37.  ************************************************************************/
  38.  
  39. #define MODULE_FILEIO
  40. #include "ray.h"
  41.  
  42. static long         *fracopp;
  43. static triangle     *scr_t;
  44. static extent       *scr_e;
  45. static sphere       *scr_s;
  46. static quad         *scr_q;
  47. static ring         *scr_r;
  48. static cylinder     *scr_c;
  49. static int          whichf;
  50.  
  51. void dumpnode(n)
  52. node    *n;
  53. {
  54.      while (n) 
  55.      {
  56.           switch (n->kind) 
  57.           {
  58.           case EXTENT :
  59.                printf("extent\n");
  60.                vecdump(eptr(n)->center,"center");
  61.                printf("radius = %f\n",eptr(n)->radius);
  62.                dumpnode(((extent *) n)->sub);
  63.                break;
  64.  
  65.           case SPHERE :
  66.                printf("sphere\n");
  67.                break;
  68.  
  69.           case TRIANGLE :
  70.  
  71.                printf("triangle\n");
  72.                break;
  73.  
  74.           case QUAD :
  75.                printf("quad\n");
  76.                break;
  77.  
  78.           case RING :
  79.                printf("ring\n");
  80.                break;
  81.  
  82.           default :
  83.  
  84.                printf("UNKNOWN KIND %d\n",n->kind);
  85.                break;
  86.           }
  87.           n = n->next;
  88.      }
  89.      printf("end\n");
  90. }
  91.  
  92. void copyattr( oa, na )
  93. attributes *oa, *na;
  94. {
  95.      na->ref = oa->ref;
  96.      na->idx = oa->idx;
  97.      na->fuz = oa->fuz;
  98.      na->tex = oa->tex;
  99.      veccopy( oa->tra, na->tra );
  100.      veccopy( oa->amb, na->amb );
  101.      veccopy( oa->dif, na->dif );
  102. }
  103.  
  104. void read_vec(v)
  105. vector  v;
  106. {
  107.      fscanf(df,"%f %f %f",&v[0],&v[1],&v[2]);
  108. }
  109.  
  110. void read_attr(attr)
  111. attributes *attr;
  112. {
  113.      fscanf(df,"%d %f %f %f",&attr->tex,&attr->fuz,&attr->ref,&attr->idx);
  114.      read_vec(attr->tra);
  115.      read_vec(attr->amb);
  116.      read_vec(attr->dif);
  117.      if (attr->tra[0] > 0.0 || attr->tra[1] > 0.0 || attr->tra[2] > 0.0)
  118.           allopaque = 0;
  119.      attr->fuz /= 10.0;  /* Assume more reasonable scale */
  120. }
  121.  
  122. void dofractal(level,a,b,c,attr)
  123. int         level;
  124. vector      a,b,c;
  125. attributes  *attr;
  126. {
  127.      float       aclen,bclen,ablen;
  128.      vector      ab,ac,bc,abbump,acbump,bcbump,v1;
  129.      long        *savedopp;
  130.  
  131.      if (level == 1) 
  132.      {
  133.           CHECK_ALLOC(scr_t,triangle);
  134.           *fracopp = (long)scr_t;
  135.           tptr(*fracopp)->next = NULL;
  136.           tptr(*fracopp)->kind = TRIANGLE;
  137.           copyattr(attr,&tptr(*fracopp)->attr);
  138.           veccopy(a,tptr(*fracopp)->position);
  139.           vecsub(c,a,ac);
  140.           veccopy(ac,tptr(*fracopp)->ve);
  141.           vecsub(b,a,ab);
  142.           veccopy(ab,tptr(*fracopp)->vp);
  143.           fracopp = (long *) &tptr(*fracopp)->next;
  144.      }
  145.      else
  146.      {
  147.           level--;
  148.           /* compute four subfaces */
  149.  
  150.           /* length of edges */
  151.           vecsub(a,c,v1);
  152.           aclen = norm(v1);
  153.           vecsub(a,b,v1);
  154.           ablen = norm(v1);
  155.           vecsub(b,c,v1);
  156.           bclen = norm(v1);
  157.  
  158.           /* edge midpoints */
  159.           vecsum(a,c,ac);
  160.           vecscale(0.5,ac,ac);
  161.           vecsum(a,b,ab);
  162.           vecscale(0.5,ab,ab);
  163.           vecsum(b,c,bc);
  164.           vecscale(0.5,bc,bc);
  165.  
  166.           /* midpoint perturbations */
  167.           noise3(ac,acbump);
  168.           if (ac[1] == 0.0)
  169.                acbump[1] = 0.0;
  170.           noise3(ab,abbump);
  171.           if (ab[1] == 0.0)
  172.                abbump[1] = 0.0;
  173.           noise3(bc,bcbump);
  174.           if (bc[1] == 0.0)
  175.                bcbump[1] = 0.0;
  176.           acbump[0] *= fractal[whichf].xscale;
  177.           acbump[1] *= fractal[whichf].yscale;
  178.           acbump[2] *= fractal[whichf].zscale;
  179.  
  180.           bcbump[0] *= fractal[whichf].xscale;
  181.           bcbump[1] *= fractal[whichf].yscale;
  182.           bcbump[2] *= fractal[whichf].zscale;
  183.  
  184.           abbump[0] *= fractal[whichf].xscale;
  185.           abbump[1] *= fractal[whichf].yscale;
  186.           abbump[2] *= fractal[whichf].zscale;
  187.  
  188.           /* scale the perturbations proportional to side length */
  189.           vecscale(aclen,acbump,acbump);
  190.           vecscale(ablen,abbump,abbump);
  191.           vecscale(bclen,bcbump,bcbump);
  192.  
  193.           /* new perturbed midpoints */
  194.           vecsum(abbump,ab,ab);
  195.           vecsum(acbump,ac,ac);
  196.           vecsum(bcbump,bc,bc);
  197.  
  198.           CHECK_ALLOC(scr_e,extent);
  199.           *fracopp = (long)scr_e;
  200.           eptr(*fracopp)->next = NULL;
  201.           eptr(*fracopp)->kind = EXTENT;
  202.           eptr(*fracopp)->sub = NULL;
  203.           savedopp = &*fracopp;
  204.           fracopp = (long *) &eptr(*fracopp)->sub;
  205.  
  206.           dofractal(level,a, ab,ac,attr);
  207.           dofractal(level,ac,bc,c, attr);
  208.           dofractal(level,ab,b, bc,attr);
  209.           dofractal(level,ac,ab,bc,attr);
  210.  
  211.           fracopp = (long *) &eptr(*savedopp)->next;
  212.      }
  213. }
  214.  
  215. void readimagefile(opp)
  216. long *opp;
  217. {
  218.      triangle    temp;
  219.      vector      a,b,c;
  220.  
  221.      while (EOF != fscanf(df,"%1s",str))
  222.           switch(str[0]) 
  223.           {
  224.           case 'D' :
  225.                fscanf(df,"%d %d",&MAXCOL,&MAXROW); 
  226.                if (MAXCOL > MAXX)
  227.                     MAXCOL = MAXX;
  228.                if (MAXROW > MAXY)
  229.                     MAXROW = MAXY;
  230.                break;
  231.           case 'R' :
  232.                fscanf(df,"%f",&maxhours);
  233.                break;
  234.           case 'N' :
  235.                fscanf(df,"%f",&idxref);
  236.                break;
  237.           case 'Z' :
  238.                fscanf(df,"%d",&histogram);
  239.                break;
  240.           case 'a' :
  241.                fscanf(df,"%f",&ambscale);
  242.